
#include <BStream>

using namespace BWE;

bool BStream::move(int offset) const
{
	int pos = this->offset();
	return seek(pos + offset);
}
bool BStream::wmove(int offset)
{
	int pos = this->offset();
	return wseek(pos + offset);
}

bool BStream::begin() const
{
	if (size() == 0)
		return false;
	return size() > 0 && offset() == 0;
}
bool BStream::end() const
{
	if (size() == 0)
		return false;
	return size() > 0 && offset() == size();
}

bool BStream::write(const char* cstr)
{
	IOMode mode = this->mode();
	if (mode & IO_Text)
	{
		if (cstr == 0)
			return false;
		int len = (int)strlen(cstr);
		return write(cstr, len);
	}
	else
	{
		if (cstr == 0)
			return write(&cstr, 1);
		int len = (int)strlen(cstr);
		return write(cstr, len + 1);
	}
}
bool BStream::write(char ch)
{
	IOMode mode = this->mode();
	if (mode & IO_Text && ch == 0)
		return false;
	return write(&ch, 1);
}
bool BStream::write(char ch, int count)
{
	if (count <= 0)
		return false;
	IOMode mode = this->mode();
	if (mode & IO_Text && ch == 0)
		return false;
	while (count-- > 0)
		write(&ch, 1);
	return true;
}

bool BStream::read(BByteArray& bytes, int size) const
{
	if (size > 0)
	{
		bytes.reset(size);
		return read(bytes.data(), size);
	}
	return false;
}
bool BStream::write(const BByteArray& bytes, int size)
{
	if (bytes.size() < size)
		return false;
	return write(bytes.data(), size);
}

bool BStream::read(BByteArray& bytes) const
{
	int offset = this->offset();
	int size = this->size();
	if (offset >= size)
		return false;
	bytes.reset(size - offset);
	return read(bytes.data(), bytes.size());
}
bool BStream::write(const BByteArray& bytes)
{
	return write(bytes.data(), bytes.size());
}
